<?php

namespace addons\Merchants\services;

use addons\Asset\common\models\Asset;
use addons\Attend\common\models\AttendCheck;
use addons\Merchants\common\models\Addons;
use common\helpers\EchantsHelper;
use common\models\common\Log;
use common\models\merchant\Merchant;
use Yii;
use addons\Employees\common\models\Employees;
use addons\Flow\common\models\Works;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use common\enums\StatusEnum;
use common\models\merchant\Member;
use common\components\Service;
use common\models\rbac\AuthRole;

/**
 * Class MemberService
 * @package addons\TinyShop\services\member
 * @author YiiFrame <21931118@qq.com>
 */
class SystemService extends Service
{

    //统计企业
    public function getMerchant()
    {
        return Merchant::find()
            ->select('id')
            ->andWhere(['>', 'status', StatusEnum::DISABLED])
            ->count();
    }

    //统计会员
    public function getMember($merchant_id = '')
    {
        return Member::find()
            ->select('id')
            ->andWhere(['>', 'status', StatusEnum::DISABLED])
            ->andFilterWhere(['merchant_id' => $merchant_id])
            ->count();
    }

    //统计员工
    public function getEmployees($merchant_id = '')
    {
        if (Addons::find()->where(['name' => 'Employees', 'merchant_id' => Yii::$app->services->merchant->getId()])->one())
            return Employees::find()
                ->select('id')
                ->andWhere(['>', 'status', StatusEnum::DISABLED])
                ->andFilterWhere(['merchant_id' => $merchant_id])
                ->count();
    }

    //待办工作
    public function getWorks($merchant_id = '')
    {
        if (Addons::find()->where(['name' => 'Flow', 'merchant_id' => Yii::$app->services->merchant->getId()])->one())
            return Works::find()
                ->select('id')
                ->where(['status' => StatusEnum::ENABLED])
                ->andWhere(['audit_id' => Yii::$app->user->id])
                ->count();
    }

    //已办工作
    public function getCompleted($merchant_id = '')
    {

        $where = new Expression("FIND_IN_SET(:supplier, audit_ids)", [":supplier" => Yii::$app->user->id]);
        if (Addons::find()->where(['name' => 'Flow', 'merchant_id' => Yii::$app->services->merchant->getId()])->one())
            return Works::find()
                ->select('id')
                ->where(['status' => StatusEnum::ENABLED])
                ->andWhere($where)
                ->count();
    }

    public function getAssets($cate_id = '')
    {
        if (Addons::find()->where(['name' => 'Asset', 'merchant_id' => Yii::$app->services->merchant->getId()])->one())
            return Asset::find()
                ->select([
                    'sum(purchase_price*num) as total',
                ])
                ->where(['>', 'status', StatusEnum::DISABLED])
//            ->andFilterWhere(['cate_id' => $cate_id])
                ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
                ->scalar();
    }

    public function getCheckIn($type)
    {
        $fields = [
            'count' => '签到次数',
        ];
        // 获取时间和格式化
        list($time, $format) = EchantsHelper::getFormatTime($type);
        // 获取数据
        if (Addons::find()->where(['name' => 'Attend', 'merchant_id' => Yii::$app->services->merchant->getId()])->one()) {
            return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
                $where = [];
                if (Yii::$app->user->identity->department_id)
                    $where = ['member_id' => Yii::$app->user->id];
                return AttendCheck::find()
                    ->select(['count(id) as count', "from_unixtime(created_at, '$formatting') as time"])
                    ->where(['>', 'status', StatusEnum::DISABLED])
                    ->andWhere(['type' => 0])
                    ->andWhere(['between', 'created_at', $start_time, $end_time])
                    ->andFilterWhere($where)
                    ->andFilterWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
                    ->groupBy(['time'])
                    ->asArray()
                    ->all();
            }, $fields, $time, $format);
        }
    }

    public function getCheckOut($type)
    {
        $fields = [
            'count' => '签到次数',
        ];
        // 获取时间和格式化
        list($time, $format) = EchantsHelper::getFormatTime($type);
        // 获取数据
        if (Addons::find()->where(['name' => 'Attend', 'merchant_id' => Yii::$app->services->merchant->getId()])->one()) {
            return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
                $where = [];
                if (Yii::$app->user->identity->department_id)
                    $where = ['member_id' => Yii::$app->user->id];
                return AttendCheck::find()
                    ->select(['count(id) as count', "from_unixtime(created_at, '$formatting') as time"])
                    ->where(['>', 'status', StatusEnum::DISABLED])
                    ->andWhere(['type' => 1])
                    ->andWhere(['between', 'created_at', $start_time, $end_time])
                    ->andFilterWhere($where)
                    ->andFilterWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
                    ->groupBy(['time'])
                    ->asArray()
                    ->all();
            }, $fields, $time, $format);
        }
    }

}